/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 * \file
 * \brief GIMnetAP name service protocol header.
 * \author Antti Maula <antti.maula@tkk.fi>
 */
#ifndef _GIMNETAP_APNAMESERVICEFRAME_H_
#define _GIMNETAP_APNAMESERVICEFRAME_H_

#include <inttypes.h>

/// Nameser
#define GIMNETAP_SERVICE_NAMESERVICE_ID    0x0101

///
#define HUBNS_NAME_MAX             32

/// Service request types.
enum EAPNameServiceRequest {
  /** Request node id by name.
   *
   */
  KAPNameServiceRequestIdByName       = 0x01,

  /** Request node name by id.
   *
   */
  KAPNameServiceRequestNameById       = 0x02,
  
  /** Register nodeid/name pair.
   *
   */
  KAPNameServiceRequestRegister       = 0x03,

  /** Unregister nodeid/name pair.
   *
   */
  KAPNameServiceRequestUnregister     = 0x04

};

/// Service flag types (These are binary flags which can be set/unset
/// separately)
enum EAPNameServiceFlag {
  KAPNameServiceFlagReply             = (1<<0), // 00000001
  KAPNameServiceFlagOverride          = (1<<1), // 00000010
  KAPNameServiceFlagNextHeader        = (1<<7), // 10000000
};

/// Service result codes.
enum EAPNameServiceResult {
  KAPNameServiceResultOK              = 0x01,
  KAPNameServiceResultNameReserved    = 0x02,
  KAPNameServiceResultNodeIdReserved  = 0x03,
  KAPNameServiceResultEntryNotFound   = 0x04,
  KAPNameServiceResultInvalidName     = 0x05,
  KAPNameServiceResultFailedUnknown   = 0xff
};

typedef struct SAPNameServiceFrame
{
  // 4 bytes
  uint16_t service_id;          ///< Service identifier
  uint8_t request;              ///< Type of request (EAPNameServiceRequest)
  uint8_t flags;                ///< Flags (combination of EAPNameServiceFlag:s)

  // 4 bytes
  int32_t identifier;           ///< Request identifier (used for matching sent/received msgs)

  // 32 bytes (4bytes * 8)
  char name[HUBNS_NAME_MAX];    ///< Name to query/register ('\0' terminated)

  // 4 bytes
  uint32_t nodeid;              ///< ID bound to name.

  // 4 bytes
  uint32_t age_left;            ///< Age left in seconds before entry expires.

  // 4 bytes
  uint8_t result;               ///< Result code (EAPNameServiceResult)
  uint8_t reserved1;            ///< Padding
  uint16_t reserved2;           ///

} TAPNameServiceFrame;
#define SIZEOF_TAPNAMESERVICEFRAME (52)


#endif
